package com.stylefeng.guns.modular.customer.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.modular.customer.dao.ContactDao;
import com.stylefeng.guns.modular.customer.dao.CustomerCompanyDao;
import com.stylefeng.guns.modular.customer.service.IContactService;
import com.stylefeng.guns.modular.customer.transfer.ContactXlsM;
import com.stylefeng.guns.modular.system.dao.ContactMapper;
import com.stylefeng.guns.modular.system.model.Contact;
import com.stylefeng.guns.modular.system.model.CustomerCompany;
import com.stylefeng.guns.modular.system.model.Purchase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.ValidationException;
import java.util.List;

import static com.stylefeng.guns.core.common.exception.BizExceptionEnum.MOBILE_NUM_EXISTED;

/**
 * <p>
 * 联系人 服务实现类
 * </p>
 *
 * @author yinjc123
 * @since 2018-04-02
 */
@Service
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact> implements IContactService {

    @Autowired
    private CustomerCompanyDao customerCompanyDao;

    @Autowired
    private ContactDao contactDao;

    @Override
    public boolean insert(Contact entity) {
        if (contactDao.selectByMobileNum(entity.getMobileNum()) != null) {
            throw new GunsException(MOBILE_NUM_EXISTED);
        }
        return super.insert(entity);
    }

    @Override
    public boolean updateById(Contact entity) {
        Contact oldContact = contactDao.selectByMobileNum(entity.getMobileNum());
        if (oldContact != null) {
            if (!oldContact.getId().equals(entity.getId())) {
                throw new GunsException(MOBILE_NUM_EXISTED);
            }
        }
        return super.updateById(entity);
    }

    @Transactional
    @Override
    public void insertXlsList(List<ContactXlsM> xlsMList) {
        for (ContactXlsM xlsM : xlsMList) {
            CustomerCompany customerCompany = customerCompanyDao.selectByUnifiedSocialCreditCode(xlsM.getUnifiedSocialCreditCode());
            if (customerCompany == null) {
                throw new ValidationException(xlsM.getCustomerCompanyName()+"不存在,统一社会信用代码为:" +  xlsM.getUnifiedSocialCreditCode());
            }
            xlsM.setCustomerCompanyId(customerCompany.getId());
        }
        for (ContactXlsM xlsM : xlsMList) {
            Contact contact = new Contact();
            BeanKit.copyProperties(xlsM,contact);
            if (contactDao.selectByMobileNum(contact.getMobileNum()) != null) {
                throw new ValidationException("手机号："+contact.getMobileNum() +",重复");
            }
            super.insert(contact);  
        }
    }

    @Override
    public void deleteByCustomerCompanyId(Integer customerCompanyId) {
        EntityWrapper<Contact> ew = new EntityWrapper<Contact>();
        ew.eq("customer_company_id", customerCompanyId);
        delete(ew);
    }

}
